<extend name="Public/base"/>

<block name="body">
    <div class="main-title">
        <h2>{:isset($info['id'])?'编辑':'新增'}物流公司</h2>
    </div>
    <div class="tab-wrap">
    	<ul class="tab-nav nav">
			<li data-tab="tab1" class="current"><a href="javascript:void(0);">基 础</a></li>
			<li data-tab="tab2"><a href="javascript:void(0);">费 用</a></li>
		</ul>
		<div class="tab-content">
    		<form action="__SELF__" method="post" class="form-horizontal" autocomplete="off">
		    	<div id="tab1" class="tab-pane in tab1">
		    		<div class="form-item">
		    			<label class="item-label">物流公司代号</label>
		    			<div class="controls">
		    				<input type="text" class="text input-2x" name="code" value="{$info.code|default=''}" placeholder="物流公司代号">
		    			</div>
		    		</div>
		    		<div class="form-item">
		    			<label class="item-label">物流公司名称</label>
		    			<div class="controls">
		    				<input type="text" class="text input-large" name="title" value="{$info.title|default=''}" placeholder="物流公司名称">
		    			</div>
		    		</div>
			        <div class="form-item">
			            <label class="item-label">官方网址</label>
			            <div class="controls">
			                <input type="text" class="text input-large" name="website" value="{$info.website|default=''}" placeholder="官方网址">
			            </div>
			        </div>
			        <div class="form-item">
			            <label class="item-label">服务电话</label>
			            <div class="controls">
			                <input type="text" class="text input-large" name="mobile" value="{$info.mobile|default=''}" placeholder="服务电话">
			            </div>
			        </div>
			        <div class="form-item">
			            <label class="item-label">基本费用</label>
			            <div class="controls">
			                <input type="text" class="text input-mini" name="price" value="{$info.price}" placeholder="元">
			            </div>
			        </div>
			        <div class="form-item">
			            <label class="item-label">排序<span class="check-tips">（用于分组显示的顺序）</span></label>
			            <div class="controls">
			                <input type="text" class="text input-small" name="sort" value="{$info.sort|default=0}">
			            </div>
			        </div>
			    </div>
			    <div id="tab2" class="tab-pane tab2">
			    	<div class="form-item">
						<div class="controls options">
							<dl class="checkmod">
								<dt class="hd">
									<label class="checkbox">请选择省份</label>
									<div class="fr">
										<a class="btn resetOption">取消选中</a>
									</div>
								</dt>
								<dd class="bd">
									<div class="rule-check option-btn">
										<volist name=":getProvinces()" id="vo">
											<div class="fl"><a class="btn" data-id="{$vo.province_id}">{$vo.province}</a></div>
										</volist>
									</div>
								</dd>
							</dl>
						</div>
					</div>
					<div class="form-item">
						<label class="item-label"><a class="btn addOptionPrice">+ 价钱</a><input type="text" class="text input-mini" placeholder="价格"></label>
						<div class="controls">
							<div class="data-table table-striped">
								<table class="">
									<thead>
										<tr>
											<th class="">省份</th>
											<th class="">价格</th>
											<th class="">操作</th>
										</tr>
									</thead>
									<tbody>
										<notempty name="info.prices">
											<volist name="info.prices" id="vo">
												<tr class="OptionPriceTR">
													<td class=""><a class="btn btn-mini">{$vo.province_id|get_table_field=###,'province_id','province','hat_province'}</a></td>
													<td class=""><input type="text" class="text input-mini" placeholder="价格" data-id="{$vo.id}" value="{$vo.price}"><span class="help-inline msg"></span></td>
													<td class=""><a class="btn delOptionPrice" data-id="{$vo.id}">删除</a></td>
												</tr>
											</volist>
										</notempty>
									</tbody>
								</table>
							</div>
						</div>
					</div>
			    </div>
		        <div class="form-item">
		            <input type="hidden" name="id" value="{$info.id|default=''}">
		            <button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">确 定</button>
		            <a class="btn btn-return" href="{:U('Shipping/index')}">返 回</a>
		        </div>
    		</form>
    	</div>
    </div>
</block>

<block name="script">
<script type="text/javascript">
//导航高亮
highlight_subnav('{:U('Shipping/index')}');
showTab();

$(function(){
	$(document).delegate(".option-btn .btn", "click", function(){
		var self = $(this);
		
		if (self.hasClass('selected')){
			self.removeClass('selected');
		} else {
			self.addClass('selected');
		}
	});
	
	$('.addOptionPrice').click(function(){
		var ids = new Array(), texts = new Array(), self = $(this), input = $(this).next('input');
		
		$.each($('.option-btn .selected'), function(i, item){
			id = $(item).attr('data-id');
			text = $(item).text();
			ids.push(id);
			texts.push(text);
		});
		
		shipping_id = "{$info.id}";
		price = input.val().trim();
		paramter = {shipping_id: shipping_id, ids: ids, price: price};
		
		$.post('{:U('Shipping/addShippingPrice')}', paramter, function(data){
			if (data.status == 1) {
				input.val('');
				
				$.each(data.data, function(i, item){
					self.closest('.form-item').find('tbody').append('<tr class="OptionPriceTR"><td class=""><a class="btn btn-mini">' + item.province + '</a></td><td class=""><input type="text" class="text input-mini" placeholder="价格" data-id="' + item.id + '" value="' + price +'"><span class="help-inline msg"></span></td><td class=""><a class="btn delOptionPrice" data-id="' + item.id + '">删除</a></td></tr>');
				});
			} else {
				updateAlert(data.message);
				setTimeout(function(){
					$('#top-alert').find('button').click();
				},1500);
			}
		}, 'json');
	});
	
	$('.resetOption').click(function(){
		$('.option-btn .selected').removeClass('selected');
	});
	
	$(document).delegate(".delOptionPrice", "click", function(){
		if(confirm("确认要删除该属性？")) {
			self = $(this);
			id = self.attr('data-id');
			paramter = {id: id};
			
			$.post('{:U('Shipping/deleteShippingPrice')}', paramter, function(data){
				if (data.status == 1) {
					self.closest('tr').remove();
				} else {
					updateAlert(data.message);
					setTimeout(function(){
						$('#top-alert').find('button').click();
					},1500);
				}
			}, 'json');
			
		}
	});
	
	$(document).delegate(".OptionPriceTR input", "click", function(){
		var self = $(this);
		self.data('param', self.val());
	});
	
	$(document).delegate(".OptionPriceTR input", "blur", function(){
		var self = $(this);
		if (self.data('param') != self.val()) {
			id = self.attr('data-id');
			price = self.val();
			paramter = {id: id, price: price};
			
			$.post('{:U('Shipping/editShippingPrice')}', paramter, function(data){
				var name = data.status ? "success" : "error", msg;
				msg = self.next(".msg").addClass(name).text(data.message).css("display", "inline-block");
				setTimeout(function(){
					msg.fadeOut(function(){
						msg.text("").removeClass(name);
					});
				}, 1000);
			}, 'json');
		}
	});
	
	$(document).delegate(".OptionPriceTR input", "keyup", function(e){
        if(e.keyCode==13){
        	$(this).blur();
        }
    });
});

document.onkeydown = function(event) {  
    var target, code, tag;  
    if (!event) {  
        event = window.event; //针对ie浏览器  
        target = event.srcElement;  
        code = event.keyCode;  
        if (code == 13) {  
            tag = target.tagName;  
            if (tag == "TEXTAREA") { return true; }  
            else { return false; }  
        }  
    }  
    else {  
        target = event.target; //针对遵循w3c标准的浏览器，如Firefox  
        code = event.keyCode;  
        if (code == 13) {  
            tag = target.tagName;  
            if (tag == "INPUT") { return false; }  
            else { return true; }   
        }  
    }  
};
</script>
</block>